﻿//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.TridentModelSL
{
    using System.Windows.Controls;
    using Microsoft.Research.ScientificWorkflow.WebClient.Views;
    using System;
    using System.ComponentModel;

    /// <summary>
    /// Main window for the web client. Holds all the views and status bar
    /// </summary>
    public partial class MainWindow : UserControl, INotifyPropertyChanged
    {
        private bool working;

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="MainWindow"/> is working.
        /// </summary>
        /// <value><c>true</c> if working; otherwise, <c>false</c>.</value>
        public bool Working
        {
            get
            {
                return this.working;
            }
            set
            {
                this.working = value;
                this.OnPropertyChanged("Working");
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the status message.
        /// </summary>
        /// <value>The status message.</value>
        public string StatusMessage
        {
            get
            {
                return this.statusBar.Message;
            }
            set
            {
                this.statusBar.Message = value;
            }
        }

        /// <summary>
        /// Loads the views.
        /// </summary>
        /// <param name="toolBarView">The tool bar view.</param>
        /// <param name="navigationView">The navigation view.</param>
        /// <param name="workAreaView">The work area view.</param>
        /// <param name="propertiesView">The properties view.</param>
        public void LoadViews(IView toolBarView, IView navigationView, IView workAreaView)
        {
            this.gridToolBarPane.Children.Clear();
            this.gridNavigationPane.Children.Clear();
            this.gridWorkAreaPane.Children.Clear();
            this.gridPropertyPane.Children.Clear();
            this.StatusMessage = string.Empty;

            if (toolBarView != null && toolBarView.View != null)
            {
                toolBarView.LaunchWaitScreen += new EventHandler(this.OnLaunchWaitScreen);
                toolBarView.RemoveWaitScreen += new EventHandler(this.OnRemoveWaitScreen);
                toolBarView.Initialize();
                this.gridToolBarPane.Children.Add(toolBarView.View);
            }
            if (navigationView != null && navigationView.View != null)
            {
                navigationView.LaunchWaitScreen += new EventHandler(this.OnLaunchWaitScreen);
                navigationView.RemoveWaitScreen += new EventHandler(this.OnRemoveWaitScreen);
                navigationView.Initialize();
                this.gridNavigationPane.Children.Add(navigationView.View);
            }
            if (workAreaView != null && workAreaView.View != null)
            {
                workAreaView.LaunchWaitScreen += new EventHandler(this.OnLaunchWaitScreen);
                workAreaView.RemoveWaitScreen += new EventHandler(this.OnRemoveWaitScreen);
                workAreaView.Initialize();
                this.gridWorkAreaPane.Children.Add(workAreaView.View);
            }
        }

        private void OnLaunchWaitScreen(object sender, EventArgs e)
        {
            this.Working = true;
        }

        private void OnRemoveWaitScreen(object sender, EventArgs e)
        {
            this.Working = false;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string changed)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(changed));
            }
        }

        #endregion
    }
}
